package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author xiaodoudou
 * @version V1.0
 * @Project ssmDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/15 10:18
 */
@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

//    分页查看
    @GetMapping
    public String queryUsers(Model model
            ,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum
            ,@RequestParam(value = "pageSize",defaultValue = "2")Integer pageSize){
        Object page = userService.findPage(pageNum, pageSize);
        model.addAttribute("data",page);
        return "/jsp/userpro.jsp";
    }


//    @GetMapping
//    public String queryUsers(Model model){
//        System.out.println("使用queryUsers");
//        List<User> users=userService.findAll();
//        model.addAttribute("users",users);
//        return "user";
//    }

//    删除
    @RequestMapping("/delete")
    public String deleteUser(Integer id){
        userService.deleteById(id);
        return "/users";
    }

//    添加
    @RequestMapping("/insert")
    public String addUser(User user){
        userService.addUser(user);
        return "/users";
    }

    @RequestMapping("/update")
    public String updateUser(Integer id,Model model){
         User user=userService.findById(id);
        model.addAttribute("user",user);
        return "/jsp/Update.jsp";
    }

    @RequestMapping("/updates")
    public String updateUser(User user){
        userService.update(user);
        return "/users";
    }
}
